/**
 * 定义统计分析模块
 */
(function(window, angular, undefined){
	var OverAnalysisModule = angular.module('water.apps.WasteWater.OverAnalysis.OverAnalysisManager', ['water.entity.PsWwtpAttr','water.entity.monitorStation','water.entity.sitePollutants','water.entity.OverAnalysis']);
	
	OverAnalysisModule.controller('OverAnalysisController',function($wdp,$rootScope,$scope,$PsWwtpAttrService,$monitorStationService,$sitePollutantsService,$overAnalysisService){
		/**
		 * 查询条件
		 */
		$scope.queryObject={};
		/**
		 * 区域数据
		 */
		$scope.areaList=[];
		/**
		 * 流域数据
		 */
		$scope.riverList=[];
		/**
		 * 污水厂数据
		 */
		$scope.wwtpList=[];
		/**
		 * 展示站点数据
		 */
		$scope.stationList=[];
		/**
		 * 所有站点数据
		 */
		$scope.AllstationList=[];
		/**
		 * 检测项数据
		 */
		$scope.pollList=[];
		/**
		 * 统计方式
		 */
		$scope.statisticalType=[{"k":"M2M","v":"环比"},{"k":"Y2Y","v":"同比"}];
		/**
		 * 数据源
		 */
		$scope.dataSoure=[{"k":"day","v":"日"},{"k":"month","v":"月"},{"k":"quarter","v":"季"},{"k":"year","v":"年"}];
		/**
		 * 数据分析项
		 */
		$scope.DataAnalysisItem=[{"k":"wwtp","v":"污水厂"},{"k":"river","v":"流域"},{"k":"xzq","v":"行政区"}];
		/**
		 * 污水厂id
		 */
		$scope.WWTP_ID=[];
		/**
		 * 站点id
		 */
		$scope.STATION_ID=[];
		
		/**
		 * 参数id
		 */
		$scope.MulitStationPollSelectCode = {};
		/**
		 * 选中统计方式
		 */
		$scope.TypeId="M2M";
		/**
		 * 数据源id
		 */
		$scope.SoureId="day";
		/**
		 * 统计长度
		 */
		$scope.statiscalLength=3;
		/**
		 * 基准时间
		 */
		$scope.baseTime="2017-12-01";
		/**
		 * 开始时间
		 */
		$scope.startDate="";
		/**
		 * 结束时间
		 */
		$scope.endDate="";
		/**
		 * 数据分析项id
		 */
		$scope.AnalysisItemId="river";
		/**
		 * 控制显示环比同比时间条件
		 */
		$scope.isShowTime=true;
		/**
		 * 初始化环比同比列表
		 */
		$("#m2my2ylistdata").idTabs();
		/**
		 * 选择参数
		 */
		$scope.onPollChange=function(e){
			
		};
		/**
		 * 选择统计方式
		 */
		$scope.onstatisticalTypeChange=function(e){
			if(e=="M2M"||e=="Y2Y"){
				$scope.isShowTime=true;
			}
			else{
				$scope.isShowTime=false;
			}
		};
		/**
		 * 选择数据源
		 */
		$scope.onDataSoureChange=function(e){
			
		};
		/**
		 * 选择数据分析项
		 */
		$scope.onDataAnalysisItemChange=function(e){
			
		};
		/**
		 * 选择污水厂
		 */
		$scope.onWwtpChange=function(e){
			//$scope._getStationData();
		};
		/**
		 * 查询
		 */
		$scope.onSearchClick=function(){
			var obj={};
			obj.baseTime=$scope.baseTime;
			$overAnalysisService.getOverRateForRiver(obj).then(function(d){
				$scope.data=d.dataList;
				$scope.dateTexts=d.dateTexts;
				$scope.month=d.month;
			}); 
		};
		/**
		 * 导出报表
		 */
		$scope.onExportExcelClick=function(){
			var obj={};
			obj.baseTime=$scope.baseTime;
			var queryString = $waterDataCommon.jsonTostring(obj);
			window.open(getBasePath()+"/OverAnalysisForWWTP/getData.do?"+queryString);
		}
		/**
		 * 获取污水厂数据
		 */
		$scope._getPsWwtpAttr=function(){
			$PsWwtpAttrService.getList($scope.queryObject).then(function(d){
				$scope.wwtpList.splice(0,$scope.wwtpList.length);
				for(var i=0;i<d.data.length;i++){
					var item = d.data[i];
					$scope.wwtpList.push({k:item.EXP_NO,v:item.NAME});
				}
				//查询检测项
				$scope._getPollList();
			});
		};
		/**
		 * 获取检测项数据
		 */
		$scope._getPollList=function(){
			$overAnalysisService.getPollList($scope.queryObject).then(function(d){
				$scope.pollList.splice(0,$scope.pollList.length);
				for(var i=0;i<d.data.length;i++){
					var item = d.data[i];
					$scope.pollList.push({k:item.NAME,v:item.S_DOMAINCAPTION});
				}
			});
		};
		/**
		 * 选择检测项
		 */
		$scope.onPollChange=function(e){
			
		};
		/**
		 * 获取站点数据
		 */
		$scope._getStationData=function(){
			$scope.queryObject.STATION_ID="";
			$monitorStationService.getList($scope.queryObject).then(function(data){
				$scope.stationList.splice(0,$scope.stationList.length);
				for(var i=0;i<data.length;i++){
					var item = data[i];
					if($scope.WWTP_ID.toString().indexOf(item.WWTP_ID) > -1) {
						$scope.stationList.push({k:item.EXP_NO,v:item.NAME});
					}
				}
			});	
		};
		/**
		 * 获取流域数据
		 */
		$scope._getRiverList=function(){
			$overAnalysisService.getRiverList($scope.queryObject).then(function(d){
				$scope.riverList.splice(0,$scope.riverList.length);
				for(var i=0;i<d.data.length;i++){
					var item = d.data[i];
					$scope.riverList.push({k:item.CODE,v:item.NAME});
				}
			});
		};
		/**
		 * 获取区划数据
		 */
		$scope._getAreaList=function(){
			$overAnalysisService.getAreaList($scope.queryObject).then(function(d){
				$scope.areaList.splice(0,$scope.areaList.length);
				for(var i=0;i<d.data.length;i++){
					var item = d.data[i];
					$scope.areaList.push({k:item.S_DOMAINCODE,v:item.S_DOMAINCAPTION});
				}
			});
		};
		/**
		 * 选择区划
		 */
		$scope.onAreaChange=function(e){
			if($scope.AREA_ID.length>0){
				var obj;
				var str="";
				for(var i=0;i<$scope.AREA_ID.length;i++){
					obj=$scope.AREA_ID[i];
					str+="'"+obj+"',"
				}
				str=str.substring(0,str.length-1);
				$scope.queryObject.AREA_ID=str;
			}
			//$scope._getRiverList();
		};
		/**
		 * 选择站点
		 */
		$scope.onStationChange=function(e){
			$scope._getStationPollData();
			
		};
		/**
		 * 选择流域
		 */
		$scope.onRiverChange=function(e){
			if($scope.RIVER_ID.length>0){
				var obj;
				var str="";
				for(var i=0;i<$scope.RIVER_ID.length;i++){
					obj=$scope.RIVER_ID[i];
					str+="'"+obj+"',"
				}
				str=str.substring(0,str.length-1);
				$scope.queryObject.RIVER_BASIN=str;
			}
			$scope._getPsWwtpAttr();
		};
		/**
		 * 获取站点参数数据
		 */
		$scope._getStationPollData=function(){
			$scope.queryObject.STATION_ID=$scope.STATION_ID.toString();
			$sitePollutantsService.getSitePollutantsParams($scope.queryObject).then(function(data){
				$scope.stationPollList.splice(0,$scope.stationPollList.length);
				for(var item in data){
					var curPollComData = [];
					var pollList = data[item];
					var stationCode = pollList[0].STATION_CODE;
					for(var i=0;i<pollList.length;i++){
						var pollItem = pollList[i];
						curPollComData.push({"k":pollItem.CODE,"v":pollItem.NAME,"stationName":item,"STATION_CODE":pollItem.STATION_CODE});
					}
					$scope.stationPollList.push(curPollComData);
				}
			});	
		};
		
		/**
		 * 获取多站点参数集合
		 */
		function _getMulitPollSelectCode(data){
			var pollCodeStr = ""; 
			for(var stationCode in data){
				pollCodeStr += "@"+stationCode+":";
				var pollSelectItem = data[stationCode];
				for(var i=0;i<pollSelectItem.length;i++){
					pollCodeStr += pollSelectItem[i]+",";
				}
			}
			return pollCodeStr;
		}
		/**
		 * 绘制图表
		 */
		function _Y2YM2Mcharts(data,charId){
			$("#"+charId).empty();
			var xDatas = [];
			
			for(var i=0;i<data.dateTimes.length;i++){
				xDatas.push(data.dateTimes[i].TIME);
			}
			
			for(var p=0;p<data.pollDeduplicated.length;p++){
				var charDiv = $("<div class='charDiv' id='"+charId+p+"'></div>").css({height: 300,width:$rootScope.Map_W-100});
				$("#"+charId).append(charDiv);
				var legendNames=[];
				var yDatas = [];
				var currentPollName = data.pollDeduplicated[p].POLLNAME;
				for(var s = 0;s<data.dataList.length;s++){
					var yDatasItem = [];
					var pollName=data.dataList[s]['PARAM'];
					if(currentPollName==pollName){
						legendNames.push(data.dataList[s].WWTP_NAME);
						for(var d=0;d<data.dataList[s].DATELIST.length;d++){
							yDatasItem.push(data.dataList[s].DATELIST[d]);
//							d=d+2;
						 }
						var item={};
						item.data=yDatasItem;
						item.type='line';
						item.name=data.dataList[s].WWTP_NAME;
						yDatas.push(item);
					}
				}
				var myChart = echarts.init(document.getElementById(charId+p));
				var option = option = {
						 title: {
						        text: currentPollName
						    },
					    tooltip: {
					        trigger: 'axis'
					    },
					    legend: {
					        data:legendNames
					    },
					    toolbox: {
					        show: false,
					        feature: {
					            dataZoom: {
					                yAxisIndex: 'none'
					            },
					            dataView: {readOnly: false},
					            magicType: {type: ['line', 'bar']},
					            restore: {},
					            saveAsImage: {}
					        }
					    },
					    xAxis:  {
					        type: 'category',
					        boundaryGap: false,
					        data: xDatas
					    },
					    yAxis: {
					        type: 'value',
					        axisLabel: {
					            formatter: '{value}% '
					        }
					    },
					    series:yDatas
					};
				myChart.setOption(option);
			}
			
		}
		//默认初次
		setTimeout(function(){
			var obj={};
			obj.baseTime=$scope.baseTime;
			$overAnalysisService.getOverRateForRiverForF(obj).then(function(d){
				$scope.data=d.dataList;
				$scope.dateTexts=d.dateTexts;
				$scope.month=d.month;
			}); 
        }, 2000);
	});
}(window, angular));